// Inserts text dynamically in the document when it is called
// Note that all text are dynamically inserted into the document
// when a call to this function are made in the BODY part of the file.
function sayHello ()
{
document.write( "I see that the time is <B>" +
getTime() +
"</B>, so I wish you" );
if(getHourOfDay()<5 || getHourOfDay()>19)
document.write('<FONT COLOR="2C396D"> a good night!</FONT>');
else
{
if ( getHourOfDay() <11)
{
document.write('<FONT COLOR="52A553"> a good morning!</FONT>');
}
else
{
document.write('<FONT COLOR="ED363C"> a good day!</FONT>');
}
}
}
// -- End of JavaScript code -------------- -->
</SCRIPT>
</HEAD>
<BODY BGCOLOR=FFFFFF TEXT=000000>
<H1>JavaScript Dynamic Document</H1>
<B>This stationery page contains JavaScript that creates a dynamic page</B>
<P>
Please note that JavaScript is currently only available in Netscape Navigator 2.0 or higher. <BR>
<FONT COLOR="931B15">Do not assume that all in your audience are using a JavaScript enabled browser.</FONT>
<HR>
Here is an example on how JavaScript can be used to provide a dynamic page that will change its contents depending upon the local time specified in the browser's system and the OS that is used by the reader.
<P>
<!-- 1) Let the reader know the browser ID used
(this is often logged by servers for keeping statistics) -->
Welcome <B><SCRIPT>
<!--
document.write(navigator.userAgent)
// -->
</SCRIPT></B>!
<P>
<!-- 2) Tell the name of the browser used -->
<SCRIPT>
<!--
document.write("Nice to see that you use ", navigator.appName," ", navigator.appVersion, ".")
// -->
</SCRIPT>
<P>
<!-- 3) Say something that depends upon the readers local time -->
<!-- This is all done in the function sayHello -->
<SCRIPT>
<!--
sayHello()
// -->
</SCRIPT>
<!-- 4) Finally, say something special to the MacOS users -->
<SCRIPT>
<!--
if (isMacintoshBrowser())
document.write(
'<P><FONT COLOR="385FD1">Very, very nice to see that you are using MacOS!</FONT>');
else
document.write(
'<P>Sad too see that you are not using a Mac to view this page.')
// -->
</SCRIPT>
<HR>
<P>
<B>How to use:</B><BR>
Four small scripts are embedded inside the text contents of this file. The scripts are executed - and inserts text - when the page is being loaded. View the source file to see how this is done. Every function is commented.
<P>
Replace the contents inside the script with calls to your own test functions and add text you want to have displayed inside <FONT COLOR="FF3366"><CODE>document.write()</CODE></FONT> function inside the script. It is recommended to create functions for complex scripts and place these in the HEAD section, see the <TT>sayHello</TT> function for an example of this.
<P>
Make sure that all text is placed inside the <FONT COLOR="FF3366"><CODE>document.write()</CODE></FONT> function, otherwise it will not look good on browsers that don't support JavaScript.
<P>
Edit this page or copy selected scripts to create your own dynamic page!
<P>
<HR>
<P>
<B>Example code for the first dynamic text above:</B>
<XMP>Welcome <B><SCRIPT>
<!--
document.write(navigator.userAgent)
// -->
</SCRIPT></B>!</XMP>
Note that the text "<B>Welcome !</B>" is the only text that will be displayed in browsers that don't support JavaScript.
<P>
You <I>may</I> also write the script without the <FONT COLOR="555555"><!--</FONT> comment (see below), but if you do this the actual script may show up in a browser that doesn't support JavaScript. This is not recommended:
The function can be used inside a script like this:
<XMP>if (isMacintoshBrowser())
document.write('Mac specific text...');
else
document.write('Text for other platforms...')
</XMP>
<P>
<HR>
<P>
<B>Bug note:</B><BR>
In this version of the script I have modified the getTime() so that it will make the time to be displayed correctly in Navigator 3.0 (and 2.0). The time seem to be offset by one hour in the 3.0 version compared to the 2.0 version! This may have something to do with daylight saving time (or maybe not...).